#include <iostream>
#include <queue>
#include <vector>
using namespace std;

const int kMaxL = 3010;
const int kInf = 1e9;

bool can_use[kMaxL];
vector<int> edges;
int dist[kMaxL];

void Dijkstra(int n) {
  priority_queue<pair<int, int>> pq;

  fill(dist, dist + n, kInf);
  dist[0] = 0;
  pq.emplace(-dist[0], 0);

  while (!pq.empty()) {
    int d = -pq.top().first, u = pq.top().second;

    pq.pop();
    if (dist[u] != d) {
      continue;
    }
    for (int e : edges) {
      int v = (u + e) % n;

      if (dist[v] > d + (u + e) / n) {
        dist[v] = d + (u + e) / n;
        pq.emplace(-dist[v], v);
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;
  int min_l = kMaxL;

  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int l;

    cin >> l;
    for (int it = l; it >= l - m && it > 0; it--) {
      can_use[it] = true;
    }
    min_l = min(min_l, max(1, l - m));
  }

  can_use[0] = true;
  for (int i = min_l + 1; i < kMaxL; i++) {
    if (!can_use[i]) {
      continue;
    }
    if (can_use[i % min_l]) {
      continue;
    }
    can_use[i % min_l] = true;
    edges.emplace_back(i);
  }

  Dijkstra(min_l);

  int ans = -1;
  for (int i = 0; i < min_l; i++) {
    if (dist[i] == kInf) {
      ans = -1;
      break;
    } else {
      ans = max(ans, (dist[i] - 1) * min_l + i);
    }
  }
  cout << ans << "\n";
  return 0;
}